package com.atguigu.java;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * @author yihu
 * @create 2021-06-01 15:20
 * @email:1057202547@qq.com
 * @dec: 测试FileInputStream和FileOutputStream的使用
 * 结论：
 *  * 1. 对于文本文件(.txt,.java,.c,.cpp)，使用字符流处理
 *  * 2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...)，使用字节流处理
 */
public class FileInoutStreamTest {
    /**
     * 图片文件复制 使用字节流
     * 使用字节流FileInputStream处理文本文件，可能出现乱码。
     */
    @Test
    public void testFileReaderFileWriterImage(){
        FileInputStream fi = null;
        FileOutputStream fo = null;
        try {
            //①创建File类的对象，指明读入和写出的文件
            File srcFileImage = new File("snipaste_20200617_145159.png");
            File desFileImage = new File(new Date().getTime()+".png");
            //创建流 输入流 输出流
            fi = new FileInputStream(srcFileImage);
            fo = new FileOutputStream(desFileImage);
            //读写
            byte[] bytes = new byte[5];
            int len = -1;
            while ((len = fi.read(bytes))!=-1){
                fo.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            try {
                if(fi!=null)
                    fi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fo!=null)
                    fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //指定路径下的文件复制
    public  void copyFile(String srcPath,String destPath){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            //复制的过程
            byte[] buffer = new byte[1024];
            int len;
            while((len = fis.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    @Test
    public void testCopyFile(){

        long start = System.currentTimeMillis();

        String srcPath = "C:\\Users\\yihu\\Desktop\\test.mp4";
        String destPath = "C:\\Users\\yihu\\Desktop\\test2.mp4";
        copyFile(srcPath,destPath);
        long end = System.currentTimeMillis();
        System.out.println("复制操作花费的时间为：" + (end - start));//34
    }

}
